#include "global.h"

///add a filed and the content to the data body
void addField(QByteArray &data,QString fieldname,const QString &content)
{
    data.append(fieldname);
    data.append(content);
    data.append("\n");
}
///fill a protocol MSGHeader
void constructHeader(MSGHeader &header,OPERATE opr,TARGET tar,int len,short s,int opt)
{
    header.operation = opr;
    header.target = tar;
    header.len = len;
    header.state = s;
    header.option = opt;
}
///input the protocol and data,the put them together and return the package
QByteArray generatePackage(MSGHeader header,const QByteArray &data)
{
    int header_len = sizeof(header);
    int package_len = header_len + header.len;

    char *package = new char[package_len];

    //put the header and the data together,generate a Data package
    memcpy(package,(char*)(&header),header_len);
    memcpy(package+header_len,data.data(),header.len);
    //package[package_len-1] = '\0';

    QByteArray Data = QByteArray::fromRawData(package, package_len);

    //delete package;
    return Data;
}

